package eu.codlab.eirb.caslibrary;

import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountManager;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TwittEirbCreateAccountActivity extends AccountAuthenticatorActivity{

	public static final String PARAM_AUTHTOKEN_TYPE = "auth.token";
	public static final String PARAM_CREATE = "create";

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		this.setContentView(R.layout.login);

		Button b = (Button)findViewById(R.login.go);
		b.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				onSaveClick(null);
			}
		});
	}

	public void onCancelClick(View v) {

		this.finish();

	}

	private ManagerAsync _manager;

	private class ManagerAsync extends AsyncTask<String, Integer, Integer>{

		@Override
		protected Integer doInBackground(String... arg0) {
			if(arg0 != null && arg0.length == 2){
				final String username=arg0[0];
				final String password =arg0[1];
				AuthenticatorHelper helper = new AuthenticatorHelper();
				String res [] = helper.infos(username, password, null);

				if (res == null) {
					runOnUiThread(new Runnable(){
						public void run(){
							Toast.makeText(TwittEirbCreateAccountActivity.this, "Error :: null", Toast.LENGTH_LONG).show();
						}
					});
				}else{
					runOnUiThread(new Runnable(){
						public void run(){
							// This is the magic that addes the account to the Android Account Manager
							final Account account = new Account(username, TwittEirbAuthenticator.ACCOUNT_TYPE);
							AccountManager accMgr = AccountManager.get(TwittEirbCreateAccountActivity.this);
							accMgr.addAccountExplicitly(account, password, null);

							// process success \o/

							final Intent intent = new Intent();
							intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, username);
							intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, TwittEirbAuthenticator.ACCOUNT_TYPE);
							intent.putExtra(AccountManager.KEY_AUTHTOKEN, TwittEirbAuthenticator.ACCOUNT_TYPE);
							TwittEirbCreateAccountActivity.this.setAccountAuthenticatorResult(intent.getExtras());
							TwittEirbCreateAccountActivity.this.setResult(RESULT_OK, intent);
							TwittEirbCreateAccountActivity.this.finish();
						}
					});
				}
				_manager=null;
			}
			return null;
		}

	}
	public void onSaveClick(View v) {
		if(_manager == null){
			TextView tvUsername;
			TextView tvPassword;
			String username;
			String password;

			tvUsername = (TextView) this.findViewById(R.login.login);
			tvPassword = (TextView) this.findViewById(R.login.password);

			tvUsername.setBackgroundColor(Color.WHITE);
			tvPassword.setBackgroundColor(Color.WHITE);

			username = tvUsername.getText().toString();
			password = tvPassword.getText().toString();
			String accountType = this.getIntent().getStringExtra(PARAM_AUTHTOKEN_TYPE);
			if (accountType == null){
				accountType = TwittEirbAuthenticator.ACCOUNT_TYPE;
			}

			_manager =new ManagerAsync();
			_manager.execute(new String[]{username, password});
		}
	}

	@Override
	public void onBackPressed(){
		finish();
	}

}